package com.jiuyou.clientController.mall.order;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.order.domain.OrderDo;
import com.ruoyi.order.domain.StoreProductOrder;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.domain.MallOrderMesh;
import com.ruoyi.shopping.service.IMallOrderMeshService;
import com.ruoyi.shopping.service.IMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author chengliang
 * @date 2022/6/29
 **/
@RestController
@RequestMapping("/client/mall/orderView")
public class OrderViewClientController extends ClientBaseController {
    @Autowired
    private IMallOrderService mallOrderService;
    @Autowired
    private IMallOrderMeshService meshService;


    @PostMapping("/getDetail/{meshId}")
    public AjaxResult getDetail(@PathVariable Long meshId){
        MallOrderMesh mallOrderMesh = meshService.selectMallOrderMeshById(meshId);
        List<MallOrder> orders = mallOrderService.selectMallOrderByMeshId(meshId);
        HashMap<String, Object> map = new HashMap<>();
        JSONObject meshJso = new JSONObject();
        BigDecimal deductionPrice = BigDecimal.ZERO;
        BigDecimal totalPrice = BigDecimal.ZERO;
        ArrayList<JSONObject> orderJson = new ArrayList<>();
        for (MallOrder order : orders) {
            JSONObject jso = new JSONObject();
            if(order.getDeductionPrice()!=null){
                deductionPrice = deductionPrice.add(order.getDeductionPrice());
            }
            totalPrice = totalPrice.add(order.getPayPrice());
            jso.put("id",order.getId());
            jso.put("productId",order.getProductId());
            jso.put("productImage",order.getProductImage());
            jso.put("productName",order.getProductName());
            jso.put("specificationName",order.getSpecificationName());
//            jso.put("createTime", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",order.getCreateTime()));
            jso.put("payPrice", Arith.numTostr(order.getPayPrice()) );
            jso.put("num",order.getNum());
            jso.put("refundStatus",order.getRefundStatus());
            jso.put("orderState",order.getOrderState());
            jso.put("commentStatus",order.getCommentStatus());
            jso.put("courierNumber",order.getCourierNumber()==null?"":order.getCourierNumber());
            String company = DictUtils.getDictLabel("logistics_company", order.getCourierCompany());
            jso.put("courierCompany", company==null?"":company);
            jso.put("afterSaleState",order.getAfterSalesState());
            orderJson.add(jso);
        }
        MallOrder mallOrder = orders.get(0);
        JSONObject addr = new JSONObject();
        addr.put("info",new StringBuilder(mallOrder.getConsigneeName()).append(" ").append(mallOrder.getConsigneePhone()).toString());
        addr.put("addr",mallOrder.getConsigneeAddress());
        map.put("address",addr);
        genMeshJson(mallOrderMesh,meshJso,deductionPrice,totalPrice);
        map.put("mesh",meshJso);
        map.put("list",orderJson);
        return AjaxResult.success(map);
    }

    private void genMeshJson(MallOrderMesh mesh, JSONObject jso, BigDecimal deductionPrice,BigDecimal totalPrice) {
        jso.put("id", mesh.getId());
        jso.put("orderId",mesh.getOrderId());
        jso.put("payState",mesh.getPayState());
        jso.put("payPrice",Arith.numTostr(mesh.getPayPrice()));
        jso.put("deductionPrice",Arith.numTostr(deductionPrice));
        jso.put("payTime",DateUtils.dateToStr(mesh.getPayTime()));
        jso.put("createTime",DateUtils.dateToStr(mesh.getCreateTime()));
        jso.put("totalPrice",Arith.numTostr(totalPrice.add(deductionPrice)));
        jso.put("orderState",mesh.getOrderState());

    }

    @GetMapping
    public AjaxResult getList(String state){
        OrderDo order = new OrderDo();
        if(!"all".equals(state)){
            switch (state){
                case "wait_pay":
                    order.setOrderState(DicConstants.ORDER_STATE_Wait_Pay.getCode());
                    order.setAftersaleState(DicConstants.SYS_N.getCode());
//                    order.setPayState(DicConstants.SYS_Y.getCode());
                    break;
                case "wait_use":
                    order.setOrderState(DicConstants.ORDER_STATE_Wait_Use.getCode());
                    order.setAftersaleState(DicConstants.SYS_N.getCode());
//                    order.setPayState(DicConstants.SYS_Y.getCode());
                    break;
                case "wait_comment":
//                    order.setCommentId(0L);
                    order.setAftersaleState(DicConstants.SYS_N.getCode());
                    order.setCommentState(DicConstants.SYS_N.getCode());
                    order.setOrderState(DicConstants.ORDER_STATE_Used.getCode());
                    break;
                case "refund_success":
                    order.setAftersaleState(DicConstants.SYS_Y.getCode());
                    break;
            }
        }
        order.setMemberId(getLoginMemberId());
        startPage();
//        List<StoreProductOrder> list = orderService.selectStoreProductOrderList(order);
        ArrayList<OrderDo> orderDos = new ArrayList<>();
        List<HashMap<String, List<JSONObject>>> maps = new ArrayList<HashMap<String, List<JSONObject>>>();
//        TableDataInfo tableDataInfo=getDataTable(list);
        orderDos.forEach(e->{
            if(e.getType().equals("M")){

            }
        });
//        List<Long> storeIds = list.stream().map(StoreProductOrder::getStoreId).distinct().collect(Collectors.toList());
//        List<Store> stores = storeService.selectStoreListByIds(storeIds);
//        List<StoreProductVo> vos = CopyUtils.copyList(list, StoreProductVo.class);
//        vos.forEach(e->{
//            stores.forEach(s->{
//                if(e.getStoreId() == s.getId()){
//                    e.set
//                }
//            });
//        });
        return AjaxResult.success("a");
    }

}
